#!/bin/bash

set -e

_default_gw=$(ip route list match 0.0.0.0 | cut '-d ' -f3)
_default_if=$(ip route list match 0.0.0.0 | cut '-d ' -f5)

_default_ipv4_addr=$(echo $(ip addr show dev $_default_if scope global | grep inet | grep -v inet6) | cut '-d ' -f 2 | cut '-d/' -f 1)
_default_ipv4_mask=$(echo $(ip addr show dev $_default_if scope global | grep inet | grep -v inet6) | cut '-d ' -f 4)
_default_hw_addr=$(echo $(ip addr show dev $_default_if scope global  | grep ether) | cut '-d ' -f 2)
_default_mtu=$(echo $(ip addr show dev $_default_if scope global  | head -n1) | cut '-d ' -f 5)

if [ -f /etc/lsb-release ]; then
    source /etc/lsb-release
fi

physicalNumber=0
coreNumber=0
logicalNumber=0
HTNumber=0
logicalNumber=$(grep "processor" /proc/cpuinfo|sort -u|wc -l)
physicalNumber=$(grep "physical id" /proc/cpuinfo|sort -u|wc -l)
coreNumber=$(grep "cpu cores" /proc/cpuinfo|uniq|awk -F':' '{print $2}'|xargs)
HTNumber=$((logicalNumber / (physicalNumber * coreNumber)))

cat <<EOF
{
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "$(echo $(hostname -I))"
        ],
        "ansible_architecture": "$(uname -m)",
        "ansible_date_time": {
            "date": "$(date -Idate)",
            "day": "$(date +%d)",
            "epoch": "$(date +%s)",
            "hour": "$(date +%H)",
            "iso8601": "$(date -Iseconds)",
            "iso8601_micro": "$(date -Ins)",
            "minute": "$(date +%M)",
            "month": "$(date +%m)",
            "second": "$(date +%S)",
            "time": "$(date +%H-%M-%S)",
            "tz": "$(date +%Z)",
            "tz_offset": "$(date +%z)",
            "weekday": "$(date +%A)",
            "weekday_number": "$(date +%w)",
            "weeknumber": "$(date +%W)",
            "year": "$(date +%Y)"
        },
        "ansible_default_ipv4": {
            "address": "${_default_ipv4_addr}",
            "alias": "${_default_if}",
            "broadcast": "${_default_ipv4_mask}",
            "gateway": "${_default_gw}",
            "interface": "${_default_if}",
            "macaddress": "${_default_hw_addr}",
            "mtu": ${_default_mtu},
            "type": "ether"
        },
        "ansible_distribution": "Unknown",
        "ansible_distribution_major_version": "16",
        "ansible_distribution_release": "xenial",
        "ansible_distribution_version": "16.04",
        "ansible_hostname": "$(hostname)",
        "ansible_kernel": "$(uname -r)",
        "ansible_machine": "$(uname -m)",
        "ansible_memfree_mb": $(($(echo $(cat /proc/meminfo  | grep -i memfree) | cut '-d ' -f 2) / 1024)),
        "ansible_memtotal_mb": $(($(echo $(cat /proc/meminfo  | grep -i memtotal) | cut '-d ' -f 2) / 1024)),
        "ansible_nodename": "$(hostname)",
        "ansible_os_family": "Linux",
        "ansible_processor_cores": ${coreNumber},
        "ansible_processor_count": ${physicalNumber},
        "ansible_processor_threads_per_core": ${HTNumber},
        "ansible_processor_vcpus": ${logicalNumber},
        "ansible_swapfree_mb": $(($(echo $(cat /proc/meminfo  | grep -i swapfree) | cut '-d ' -f 2) / 1024)),
        "ansible_swaptotal_mb": $(($(echo $(cat /proc/meminfo  | grep -i swaptotal) | cut '-d ' -f 2) / 1024)),
        "ansible_system": "Linux",
        "ansible_uptime_seconds": $(cat /proc/uptime | cut '-d ' -f1),
        "ansible_user_dir": "$(pwd)",
        "ansible_user_gid": $(id -g $USER),
        "ansible_user_id": "$(whoami)",
        "ansible_user_shell": "$(getent passwd $LOGNAME | cut -d: -f7)",
        "ansible_user_uid": $(id -u $USER)
    },
    "changed": false
}
EOF

exit 0
